<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <SelectWarehouse class="selectClass" v-model="form.warehouse" placeholder="仓库" :multiple="true"></SelectWarehouse>
        <el-select v-model="form.use_status" placeholder="状态" style="max-width: 150px">
          <el-option v-for="item in options['base.containerManage']?.[0].containerManagesUseStatus" :key="item.value" :label="item.label"
            :value="item.value"></el-option>
        </el-select>
        <el-select v-model="form.status" placeholder="是否启用" style="max-width: 150px">
          <el-option v-for="item in options['base.containerManage']?.[0].containerManagesStatus" :key="item.value" :label="item.label"
            :value="item.value"></el-option>
        </el-select>
        <el-input v-model="form.warehouseRang" placeholder="周转箱号" prefix-icon="search" clearable class="w-40" />
        <VGroup>
          <el-select class="selectClass" v-model="form.timeRange.type" style="max-width: 100px">
            <el-option v-for="item in timeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-date-picker v-model="form.timeRange.value" type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime()" style="max-width: 300px" />
        </VGroup>

      </template>
      <template #toolbar_btns>
        <el-button type="primary" @click="open('add')"><svg-icon class="text-white mr-1" icon-class="add"/>新增</el-button>
        <el-button type="warning" @click="handlePrint">打印</el-button>
        <el-button class="ml-auto" @click="handleImport"><svg-icon icon-class="download01" />导入</el-button>
      </template>
    </VTable>
    <ComModal ref="ComModalRef" :options="options" @query="query"></ComModal>
    <importFile ref="importRef" type="swing-box" @query="query"></importFile>
  </VPage>
</template>
<script setup name="BaseSwing-box" lang="jsx">
import { ElMessage } from 'element-plus'
import { nextTick, ref, reactive, toRefs } from 'vue'
import { getEnums } from '@/api/public'
import { queryContainerManageList } from './api'
import { getWareData } from '@/api/public'
import ComModal from './components/ComModal.vue'
import importFile from '@/components/importFile';
import SelectWarehouse from '@/components/SelectWarehouse'
import { getLodop } from '@/utils/LodopFuncs.js';


const timeOption = [
  { label: "创建时间", value: "create_time" },
  { label: "修改时间", value: "update_time" },
]

const tableRef = ref()
const tableOptins = reactive({
  rowConfig: { height: 60 },
  formConfig: {
    data: {
      status: '',
      warehouseRange: { type: 'warehouse_name', value: [], range: true },
      timeRange: { type: 'create_time', value: [], range: true },
    }
  },
  columns: [
    {
      type: 'checkbox', width: "40"
    },
    {
      type: "seq", title: '序号', width: 80
    },
    {
      field: 'container_number', title: '周转箱编号'
    },
    {
      field: 'warehouse_name', title: '所属仓库'
    },
    {
      field: 'job_number', title: '任务编号'
    },
    {
      field: 'use_status_cn', title: '状态', width: 80
    },
    {
      field: 'status_cn', title: '是否启用', width: 80
    },
    {
      field: 'create_user', title: '创建人'
    },
    {
      field: 'create_time', title: '创建日期'
    },
    {
      field: 'update_user', title: '最近修改人'
    },
    {
      field: 'update_time', title: '修改日期'
    },
    {
      title: '操作', width: "120", fixed: "right", slots: {
        default: ({ row }) => {
          return <div class="table-option">
            <div>
              <el-link type="primary" underline={false} class="border-right" onClick={() => open('edit', row.id)}>修改</el-link>
            </div>
          </div >
        }
      }
    }
  ],
  proxyConfig: {
    seq: true,
    props: {
      result: 'data',
      total: 'total',
    },
    ajax: {
      query: async ({ page, form }) => {
        const res = await queryContainerManageList({ ...form, ...page })
        return new Promise(function (resolve) {
          resolve({
            data: res.data,
            total: res.total
          })
        })
      }
    }
  }
})
const query = () => {
  tableRef.value.query()
}

const data = reactive({
  options: [],
  wareDataList: []
})
const { options, wareDataList } = toRefs(data)
const fetchOption = async () => {
  getEnums({
    version: "v1", "name": [
      "base.containerManage"
    ]
  }).then(res => {
    options.value = res.data
  })
}
fetchOption()
provide('options', options)

// 获取仓库
const getWareDataList = async () => {
  try {
    const res = await getWareData()
    wareDataList.value = res.data
  } catch (e) {
    throw Error(e)
  }
}
getWareDataList()
provide('wareDataList', wareDataList)

const ComModalRef = ref()
const open = (type, id) => {
  ComModalRef.value.open(type, id)
}

// 导入
const importRef = ref()
const handleImport = () => {
  nextTick(() => {
    importRef.value.open()
  })
}

// 打印
const handlePrint = () => {
  const records = getSelectRow()
  if (records.length == 0) {
    ElMessage.error('至少选择一项')
    return
  }
  const ids = records.map(i => i.id)
  console.log(ids)
  const LODOP = getLodop();
  // LODOP.ADD_PRINT_RECT(0, 0, "100.01mm", "60.01mm", 0, 1);
  LODOP.ADD_PRINT_TEXT(20, 50, 250, 33, "A00010");
  LODOP.SET_PRINT_STYLEA(0, "FontSize", 50);
  LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
  LODOP.ADD_PRINT_BARCODE(100, 50, 225, 85, "Code39", "A00010");
  LODOP.PREVIEW(); // 打印预览
}

//获取已选中的行
const getSelectRow = () => {
  return tableRef?.value.$table.getCheckboxRecords()
}
</script>